package com.navercorp.pinpoint.plugin.businessMethod;

import com.navercorp.pinpoint.bootstrap.context.MethodDescriptor;
import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.interceptor.SpanEventSimpleAroundInterceptorForPlugin;
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.common.util.ArrayUtils;

/**
 * Created by Irany 2018/8/22 16:18
 */
public class GeneralConfigInterceptor extends SpanEventSimpleAroundInterceptorForPlugin {
    private final PLogger logger = PLoggerFactory.getLogger(this.getClass());

    public GeneralConfigInterceptor(TraceContext context, MethodDescriptor methodDescriptor) {
        super(context, methodDescriptor);
    }

    @Override
    protected void doInBeforeTrace(SpanEventRecorder recorder, Object target, Object[] args) {
    }

    @Override
    protected void doInAfterTrace(SpanEventRecorder recorder, Object target, Object[] args, Object result, Throwable throwable) {
        recorder.recordServiceType(GeneralConfigConstants.GENERAL_CONFIG_SERVICE_TYPE);
        if (args != null && ArrayUtils.hasLength(args)) {
            recorder.recordApi(getMethodDescriptor(), args);
            recorder.recordAttribute(GeneralConfigConstants.GENERAL_CONFIG_ARG, args);
        } else {
            recorder.recordApi(getMethodDescriptor());
        }
        recorder.recordAttribute(GeneralConfigConstants.GENERAL_CONFIG_RESULT, result);
        recorder.recordException(throwable);
    }
}